//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package appstore_api_serv

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/appstore_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/appstore_api"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *AppstoreApiImpl) AgreeApp(ctx context.Context, req *appstore_api.AgreeAppRequest) (*appstore_api.AgreeAppResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &appstore_api.AgreeAppResponse{
			Code:   appstore_api.AgreeAppResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	//检查应用是否存在
	exist, err := appstore_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &appstore_api.AgreeAppResponse{
			Code:   appstore_api.AgreeAppResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	//检查是否已经点赞
	exist, err = appstore_dao.AgreeDao.Exist(ctx, req.AppId, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	if exist {
		return &appstore_api.AgreeAppResponse{
			Code: appstore_api.AgreeAppResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = appstore_dao.AgreeDao.Insert(sessCtx, &appstore_dao.AgreeDbItem{
			AppId:  req.AppId,
			UserId: sessionItem.UserId,
		})
		if err != nil {
			return nil, err
		}
		err = appstore_dao.AppDao.IncAgreeCount(sessCtx, req.AppId)
		if err != nil {
			return nil, err
		}
		return &appstore_api.AgreeAppResponse{
			Code: appstore_api.AgreeAppResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*appstore_api.AgreeAppResponse), nil
}

func (apiImpl *AppstoreApiImpl) CancelAgreeApp(ctx context.Context, req *appstore_api.CancelAgreeAppRequest) (*appstore_api.CancelAgreeAppResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &appstore_api.CancelAgreeAppResponse{
			Code:   appstore_api.CancelAgreeAppResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	//检查应用是否存在
	exist, err := appstore_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &appstore_api.CancelAgreeAppResponse{
			Code:   appstore_api.CancelAgreeAppResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	//检查是否已经点赞
	exist, err = appstore_dao.AgreeDao.Exist(ctx, req.AppId, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &appstore_api.CancelAgreeAppResponse{
			Code: appstore_api.CancelAgreeAppResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		removed, err := appstore_dao.AgreeDao.Remove(sessCtx, req.AppId, sessionItem.UserId)
		if err != nil {
			return nil, err
		}
		if removed {
			err = appstore_dao.AppDao.DecAgreeCount(sessCtx, req.AppId)
			if err != nil {
				return nil, err
			}
		}
		return &appstore_api.CancelAgreeAppResponse{
			Code: appstore_api.CancelAgreeAppResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*appstore_api.CancelAgreeAppResponse), nil
}
